package nl.renedegroot.android.intervalbeeper;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Vibrator;

public class SoundTimer
{
   private static final String TAG = "VisualTimerTask";
   MediaPlayer mMediaPlayer;
   int mSoundId;
   Uri mFileUri;
   Context mContext;
   protected String mSoundname;
   private long mPeriod;
   private long mDelay;
   private PlayerTimerTask mTask;
   private Timer mTimer;
   private long mVibratePeriod;
   private Vibrator mVibrator;

   /**
    * Constructor: create a new SoundTimer.
    * 
    * @param context
    * @param soundName
    * @param period
    * @param delay
    */
   public SoundTimer(Context context, String soundName, long period, long delay)
   {
      mContext = context;
      setSoundname(soundName);
      setPeriod(period);
      setDelay(delay);
      mTask = new PlayerTimerTask();
   }

   /**
    * Constructor: create a new SoundTimer based on a sound resource id
    * 
    * @param context
    * @param soundName
    * @param period
    * @param delay
    * @param soundId
    */
   public SoundTimer(Context context, String soundName, long period, long delay, int soundId)
   {
      this(context, soundName, period, delay);
      mSoundId = soundId;
   }

   /**
    * Constructor: create a new SoundTimer based on a local file Uri
    * 
    * @param context
    * @param soundName
    * @param period
    * @param delay
    * @param fileUri
    */
   public SoundTimer(Context context, String soundName, long period, long delay, Uri fileUri)
   {
      this(context, soundName, period, delay);
      mFileUri = fileUri;
   }
   
   public SoundTimer(Context context, String soundName, long period, long delay, long vibratePeriod)
   {
      this(context, soundName, period, delay);
      mVibratePeriod = vibratePeriod;
   }

   public void setTimer( Timer timer )
   {
      mTimer = timer;
      if( mTimer != null )
      {
         schedule();
      }
   }

   private void schedule()
   {
      if (mFileUri != null)
      {
         mMediaPlayer = MediaPlayer.create(mContext, mFileUri);
      }
      else if( mSoundId > 0 )
      {
         mMediaPlayer = MediaPlayer.create(mContext, mSoundId);

      }
      else if( mVibratePeriod > 0 )
      {
         mVibrator = (Vibrator)mContext.getSystemService(Context.VIBRATOR_SERVICE);
      }
      
//      Log.d( TAG, "Scheduling over "+getDelay()/1000+" every "+getPeriod()/1000);
      if( getPeriod() == 0 )
      {
         mTimer.schedule(mTask, getDelay());
      }
      else
      {
         mTimer.scheduleAtFixedRate(mTask, getDelay(), getPeriod());
      }
   }

   public void cancel()
   {
      if (mMediaPlayer != null)
      {
         mMediaPlayer.release();
      }
      mTask.cancel();
      mTask = new PlayerTimerTask();
   }

   @Override
   public String toString()
   {
      String description;
      if (getDelay() > 0)
      {
         if (getPeriod() > 0)
         {
            description = String.format(mContext.getString(R.string.play_after_every), getDelay() / 1000, getSoundname(), getPeriod() / 1000);
         }
         else
         {
            description = String.format(mContext.getString(R.string.play_after_once), getDelay() / 1000, getSoundname());
         }
      }
      else
      {
         if (getPeriod() > 0)
         {
            description = String.format(mContext.getString(R.string.play_every), getSoundname(), getPeriod() / 1000);
         }
         else
         {
            description = String.format(mContext.getString(R.string.play_once), getSoundname());
         }
      }
      return description;
   }

   public String getSoundname()
   {
      return mSoundname;
   }

   public void setSoundname(String soundname)
   {
      this.mSoundname = soundname;
   }

   public void setDelay(long mDelay)
   {
      this.mDelay = mDelay;
      if( mTimer != null )
      {
         cancel();
         schedule();
      }
   }

   public long getDelay()
   {
      return mDelay;
   }

   public long getPeriod()
   {
      return mPeriod;
   }

   public void setPeriod(long period)
   {
      this.mPeriod = period;
      if( mTimer != null )
      {
         cancel();
         schedule();
      }
   }

   private class PlayerTimerTask extends TimerTask
   {
      @Override
      public void run()
      {
         if( mMediaPlayer != null )
         {
//         Log.d(TAG, "TimerTask#run()");
            mMediaPlayer.seekTo(0);
            mMediaPlayer.start();
         }
         else if( mVibrator != null )
         {
            mVibrator.vibrate( mVibratePeriod );
         }
      }
   }

}
